package com.ffyc.cms.controller;

import com.ffyc.cms.common.CommonResult;
import com.ffyc.cms.model.Admin;
import com.ffyc.cms.model.Newstype;
import com.ffyc.cms.service.NewstypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;
@RestController
@RequestMapping(path = "newstype")
public class NewstypeController {
    @Autowired
    NewstypeService newstypeService;
    /**
     * 查询出所有的导航
     * @return
     */
    @RequestMapping(path = "getnewtpyeList")
    public CommonResult getnewtpyeList(){
        CommonResult commonResult=null;
        try{
            List<Newstype> newstypeslist = newstypeService.getnewtpyeList();
            if (newstypeslist==null){
                commonResult=new CommonResult(201,"新闻类型查询为空",null);
            }else {
                commonResult=new CommonResult(200,"新闻类型查询成功",newstypeslist);
            }
        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"新闻类型查询失败",null);
        }
        return commonResult;
    }

    /**
     * 根据Id删除新闻类型
     * @param id
     * @return
     */
    @RequestMapping(path = "deletenewtpye")
    public CommonResult deletenewtpye(Integer id){
        CommonResult commonResult=null;
        try{
            newstypeService.deletenewtpye(id);
            commonResult=new CommonResult(200,"删除成功",null);

        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"服务器忙请稍后重试！",null);
        }
        return commonResult;
    }

    /**
     * 添加时判断是否已存在新闻类型名
     * @param journame
     * @return
     */
    @RequestMapping(path = "checkName")
    public CommonResult checkName(String journame){
        CommonResult commonResult=null;
        try{
            boolean isContinue = newstypeService.checkName(journame);
            if (isContinue){
                commonResult=new CommonResult(201,"该新闻类型已存在,请重新输入或返回!",null);
            }else {
                commonResult=new CommonResult(200,"可以添加该新闻类型",null);
            }
        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"服务器忙请稍后重试！",null);
        }
        return commonResult;
    }

    /**
     * 添加时判断是否已存在新闻类型名
     * @param newstype
     * @return
     */
    @RequestMapping(path = "savenewstype")
    public CommonResult savenewstype(Newstype newstype, HttpSession session){
        CommonResult commonResult=null;
        try{
            Admin admin = (Admin)session.getAttribute("admin");
            newstype.setAdmin(admin);
            newstypeService.savenewstype(newstype);
            commonResult=new CommonResult(200,"保存成功",null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"服务器忙请稍后重试！",null);
        }
        return commonResult;
    }

    /**
     * 根据Id查找新闻类型
     * @param id
     * @return
     */
    @RequestMapping(path = "getnewstypeById")
    public CommonResult getnewstypeById(Integer id){
        CommonResult commonResult=null;
        try{
            Newstype newstype = newstypeService.getnewstypeById(id);
            commonResult=new CommonResult(200,"查找成功",newstype);
        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"服务器忙请稍后重试！",null);
        }
        return commonResult;
    }

    @RequestMapping(path = "updateNewsType")
    public CommonResult updateNewsType(Newstype newstype, HttpSession session){
        CommonResult commonResult=null;
        try{
            Admin admin = (Admin)session.getAttribute("admin");
            newstype.setAdmin(admin);
            newstypeService.updateNewsType(newstype);
            commonResult=new CommonResult(200,"修改成功",null);
        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"服务器忙请稍后重试！",null);
        }
        return commonResult;
    }
}
